package com.magicborrow.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.UIHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class ICanLendAdapter extends RefreshAdapter<StuffBean> {
    private int checkedPosition = -1;

    public int getCheckedPosition() {
        return checkedPosition;
    }

    public ICanLendAdapter(ArrayList<StuffBean> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateItem(ViewGroup parent, int viewType) {
        return new PublishedRentViewHolder(LayoutInflater.from(context).inflate(R.layout.icantlend_stuff_item, null));
    }

    @Override
    public void onBindItem(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof PublishedRentViewHolder) {
            PublishedRentViewHolder pHolder = (PublishedRentViewHolder) holder;
            StuffBean stuff = data.get(position);
            if (stuff.getImages().size() > 0) {
                pHolder.pic.setImageDrawable(context.getResources().getDrawable(R.mipmap.img_default));
                ImageLoader.getInstance().displayImage(Constants.ADDRESS + stuff.getImages().get(0).getPreview(), pHolder.pic);
            }
            pHolder.title.setText(stuff.getName());
            pHolder.checkBox.setTag(position);
            pHolder.price.setText("￥" + stuff.getPrice() + PriceModeEnum.getModeStr(stuff.getPriceMode()));
            if (checkedPosition == position) {
                pHolder.checkBox.setChecked(true);
            }

            pHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (recyclerView != null) {
                            CheckBox box = (CheckBox) recyclerView.findViewWithTag(checkedPosition);
                            if (box != null)
                                box.setChecked(false);
                        }
                        checkedPosition = position;


                    } else {
                        checkedPosition = -1;
                    }

                }
            });


        }


    }

    public class PublishedRentViewHolder extends RecyclerView.ViewHolder {
        private ImageView pic;
        private TextView title;
        private TextView price;
        private CheckBox checkBox;

        public PublishedRentViewHolder(View view) {
            super(view);
            pic = (ImageView) view.findViewById(R.id.pic);
            title = (TextView) view.findViewById(R.id.tv_title);
            price = (TextView) view.findViewById(R.id.price);
            checkBox = (CheckBox) view.findViewById(R.id.cb);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) pic.getLayoutParams();
            int width = UIHelper.getScreenWidth(context) / 2 - 2 * context.getResources().getDimensionPixelOffset(R.dimen._7dp) - 2 * context.getResources().getDimensionPixelOffset(R.dimen._5dp)
                    - context.getResources().getDimensionPixelSize(R.dimen._6dp);
            params.height = (int) (15.0 / 16 * width);
            //   params.width = width;
            pic.setLayoutParams(params);
        }
    }

}
